package com.zhihesj.anan.system.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhihesj.anan.system.entity.Menu;
import com.zhihesj.anan.system.mapper.MenuMapper;
import com.zhihesj.anan.system.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhihesj.anan.system.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author anan
 * @since 2020-07-22
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {


    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> findMenuByUserId(Long userId) {
        return menuMapper.findMenuByUserId(userId);
    }

    @Override
    public List<Menu> selectPage(MenuVo menuVo) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        if (menuVo.getParentId()!=null) {
            wrapper.eq("parent_id", menuVo.getParentId());
        }
        if (menuVo.getMenuName()!=null) {
            wrapper.like("menu_name", menuVo.getMenuName());
        }
        if (menuVo.getVisible()!=null) {
            wrapper.eq("visible", menuVo.getVisible());
        }
        if (menuVo.getMenuType()!=null) {
            wrapper.in("menu_type",menuVo.getMenuType().split(","));
        }

        if (menuVo.getPage()==null){
            return menuMapper.selectList(wrapper);
        }else{
            IPage<Menu> iPage =new Page<>(menuVo.getPage(),menuVo.getLimit());
            return menuMapper.selectPage(iPage, wrapper).getRecords();
        }
    }

    @Override
    public List<Menu> selectRoleKey() {
        return menuMapper.selectRoleKey();
    }
}
